package com.smartx.app.util;

import android.text.TextUtils;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

public class DataRequestUtil {
    private final static String TAG = DataRequestUtil.class.getSimpleName();
    private static DataRequestUtil requestUtil;
    private DataRequestUtil() {
    }
    public static DataRequestUtil getInstance() {
        if (requestUtil == null) {
            requestUtil = new DataRequestUtil();
        }
        return requestUtil;
    }
    public interface Callback {
        void onSucceeded(String json);
        void onFailed(String msg, int resultCode);
    }
    public void requestPost(final String url, String json, final Callback callback) {
        requestPost(url, json, callback, true);
    }

    public void requestPost(final String url, String json, final Callback callback, final boolean showNetworkError) {
        Log.i(TAG, url + ":" + json);
        HttpRequestManager.manager().postJson(json, new DefaultHttpRequest() {

            @Override
            public String url() {
                return url;
            }

            @Override
            public void onSuccess(String json) {
                getDataSuccess(url, callback, json);
            }

            @Override
            public void onFaliled(ErrorCode error) {
                super.onFaliled(error);
                if (callback != null) {
                    callback.onFailed(error.msg, error.errorCode);
                }
            }

            @Override
            protected boolean showNetworkError() {
                return showNetworkError;
            }
        });
    }

    public void requestJavaPost(final String url, String json, final Callback callback) {
        Log.i(TAG, url + ":" + json);
        HttpRequestManager.manager().postJson(json, new DefaultHttpRequest() {

            @Override
            public String url() {
                return url;
            }

            @Override
            public void onSuccess(String json) {
                parseJavaDataSuccess(callback, json);
            }

            @Override
            public void onFaliled(ErrorCode error) {
                super.onFaliled(error);
                if (callback != null) {
                    // FIXME: 20/04/2017 dalizhang
                    callback.onFailed("服务器开小差了，请稍后再试", error.errorCode);
                }
            }
        });
    }

    public void getDataSuccess(String url, Callback callback, String data) {
        Log.i(TAG, data);
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(data);
            int result = jsonObject.getInt("error_code");
            String msg = jsonObject.optString("error_msg");
            if (result == 0) {
                if (callback != null) {
                    callback.onSucceeded(jsonObject.optString("data"));
                }
            } else {
                if (callback != null) {
                    callback.onFailed(TextUtils.isEmpty(msg) ? "服务器开小差了，请稍后再试" : msg, result);
                }
            }
        } catch (JSONException e) {
            if (callback != null) {
                callback.onFailed("数据解析异常", -1);
            }
            e.printStackTrace();
        }
    }

    private void parseJavaDataSuccess(Callback callback, String data) {
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(data);
            int result = jsonObject.getInt("code");
            String msg = jsonObject.optString("msg");
            if (result == 0) {
                if (callback != null) {
                    callback.onSucceeded(jsonObject.optString("data"));
                }
            } else {
                if (callback != null) {
                    callback.onFailed(TextUtils.isEmpty(msg) ? "服务器开小差了，请稍后再试" : msg, result);
                }
            }
        } catch (JSONException e) {
            if (callback != null) {
                callback.onFailed("数据解析异常", -1);
            }
            e.printStackTrace();
        }
    }
}